import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;




public class FacPersonalInfo extends JFrame {
	public FacPersonalInfo(String user){
		getContentPane().add(new Panel(user));
		setPreferredSize(new Dimension(670, 670));
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
		




  private class Panel extends JPanel {
	private JTextField textPermAddrs;
	private JTextField textName;
	private JTextField textDob;
	private JTextField textAddrs;
	private JTextField textNumber;
	private JTextField textEmail;
	private JTextField textResearch;

	/**
	 * Create the panel.
	 */
	public Panel(final String user) {
		setLayout(null);
		setBackground(Color.ORANGE);
		JLabel label = new JLabel("");
		label.setBounds(236, 5, 0, 0);
		add(label);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(59, 107, 61, 16);
		add(lblName);
		
		JLabel lblDateOfBirth = new JLabel("Date of Birth");
		lblDateOfBirth.setBounds(59, 135, 79, 16);
		add(lblDateOfBirth);
		
		JLabel lblGender = new JLabel("Gender");
		lblGender.setBounds(59, 163, 61, 16);
		add(lblGender);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(59, 196, 61, 16);
		add(lblAddress);
		
		JLabel lblPermanentAddress = new JLabel("Permanent Address");
		lblPermanentAddress.setBounds(59, 229, 128, 16);
		add(lblPermanentAddress);
		
		JLabel lblContactNumber = new JLabel("Contact Number");
		lblContactNumber.setBounds(59, 257, 112, 16);
		add(lblContactNumber);
		
		JLabel lblEmailAddress = new JLabel("Email Address");
		lblEmailAddress.setBounds(59, 285, 89, 16);
		add(lblEmailAddress);
		
		JLabel lblDepartment = new JLabel("Department");
		lblDepartment.setBounds(59, 313, 89, 16);
		add(lblDepartment);
		
		JLabel lblPosition = new JLabel("Position");
		lblPosition.setBounds(59, 346, 61, 16);
		add(lblPosition);
		
		JLabel lblCourse = new JLabel("Course");
		lblCourse.setBounds(59, 379, 61, 16);
		add(lblCourse);
		
		JLabel lblSection = new JLabel("Section");
		lblSection.setBounds(59, 412, 61, 16);
		add(lblSection);
		
		JLabel lblResearchInterests = new JLabel("Research Interests");
		lblResearchInterests.setBounds(59, 445, 161, 16);
		add(lblResearchInterests);
		
		JLabel lblNewLabel_5 = new JLabel("");
		lblNewLabel_5.setIcon(new ImageIcon(FacPersonalInfo.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		lblNewLabel_5.setBounds(470, 0, 201, 164);
		getContentPane().add(lblNewLabel_5);
		
		textPermAddrs = new JTextField();
		textPermAddrs.setBounds(199, 223, 178, 28);
		add(textPermAddrs);
		textPermAddrs.setColumns(10);
		
		textName = new JTextField();
		textName.setBounds(199, 101, 178, 28);
		add(textName);
		textName.setColumns(10);
		
		textDob = new JTextField();
		textDob.setBounds(199, 129, 178, 28);
		add(textDob);
		textDob.setColumns(10);
		
		final JComboBox comboGender = new JComboBox();
		comboGender.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female"}));
		comboGender.setBounds(199, 159, 178, 27);
		add(comboGender);
		
		textAddrs = new JTextField();
		textAddrs.setBounds(199, 190, 178, 28);
		add(textAddrs);
		textAddrs.setColumns(10);
		
		textNumber = new JTextField();
		textNumber.setBounds(199, 251, 178, 28);
		add(textNumber);
		textNumber.setColumns(10);
		
		textEmail = new JTextField();
		textEmail.setBounds(199, 279, 178, 28);
		add(textEmail);
		textEmail.setColumns(10);
		
		final JComboBox comboBox_3 = new JComboBox();
		comboBox_3.setBounds(199, 375, 178, 27);
		add(comboBox_3);
		
		final JComboBox comboDept = new JComboBox();
		String[] dept={"Aerospace Engineering", "Biology", "Biomedical Engineering", "Computer Science", "Electrical & Computer Engineering"};
		comboDept.setModel(new DefaultComboBoxModel(dept));
		comboDept.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> code= new ArrayList<String>();
				try{
					
	            	 Connection con = Connect.getConnection();
	            	 Statement statement =con.createStatement();
	            	 String check="";
	            	 if(comboDept.getSelectedItem().toString().equals("Biomedical Engineering")){
	     				check="BME%";
	     			}
	     			else if(comboDept.getSelectedItem().toString().equals("Electrical & Computer Engineering")){
	     				check="ECE%";
	     			}
	     			else if(comboDept.getSelectedItem().toString().equals("Biology")){
	     				check="BIO%";
	     			}
	     			else if(comboDept.getSelectedItem().toString().equals("Computer Science")){
	     				check="CS%";
	     			}
	     			else if(comboDept.getSelectedItem().toString().equals("Aerospace Engineering")){
	     				check="AE%";
	     			}
//	            	  
	            	 ResultSet result = statement.executeQuery("SELECT Code FROM DEPT AS D,CRS_CODE AS CR,OFFERS AS O WHERE D.Name='" + comboDept.getSelectedItem().toString() +"' AND D.Dept_id=O.Dept_id AND O.Course_Title = CR.Course_Title AND Code LIKE '"+check+"%'");
				     
	            	 while(result.next()){
					code.add(result.getString(1));
				}
	         
	            	 String[] crs = new String[code.size()];
	            	 for(int i=0;i<code.size();i++){
	            		 crs[i]=code.get(i);
	            	 }
	            	 comboBox_3.setModel(new DefaultComboBoxModel(crs));
				}
				catch(Exception eb){
					//JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
				}finally {
				}
				
			}
		});
		comboDept.setBounds(199, 309, 178, 27);
		add(comboDept);
		
		final JComboBox comboPos = new JComboBox();
		String[] pos={"Professor", "Associate Professor", "Assistant Professor"};
		comboPos.setModel(new DefaultComboBoxModel(pos));
		comboPos.setBounds(199, 342, 178, 27);
		add(comboPos);
		
		
		
		final JCheckBox A = new JCheckBox("A");
		final JCheckBox B = new JCheckBox("B");
		final JCheckBox C = new JCheckBox("C");
		final JCheckBox D = new JCheckBox("D");
		
		final JCheckBox[] sections= {A,B,C,D};
		final String[] charSections ={"A","B","C","D"};
		A.setBounds(199,400,50,50);
		B.setBounds(253,400,50,50);
		C.setBounds(307,400,50,50);
		D.setBounds(361,400,50,50);
		add(A);
		add(B);
		add(C);
		add(D);
		
		textResearch = new JTextField();
		textResearch.setBounds(199, 439, 178, 28);
		add(textResearch);
		textResearch.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(390, 505, 117, 29);
		add(btnSubmit);
		
		JButton btnFacServices = new JButton("Faculty Service");
		btnFacServices.setBounds(200, 505, 117, 29);
		btnFacServices.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
            	dispose();
            	new FacultyServices(user);
            }
            });
		add(btnFacServices);
		
		JLabel lblYyyymmdd = new JLabel("YYYY-MM-DD");
		lblYyyymmdd.setBounds(377, 135, 117, 16);
		getContentPane().add(lblYyyymmdd);
		
		JLabel label_1 = new JLabel("");
		label_1.setBounds(0, 0, 513, 54);
		add(label_1);
		try{
			Connection con = Connect.getConnection();
			Statement stat =con.createStatement();
			String add="";
			Integer id=null;
			ResultSet result = stat.executeQuery("SELECT * FROM FACULTY WHERE Username='"+user+"'");
		    while(result.next()){
		    	if((add=result.getString(3))!=null)
		       		   textAddrs.setText(add);
		    	if((add=result.getString(4))!=null)
		       		   textName.setText(add);
		    	if((add=result.getString(5))!=null)
	        		   textEmail.setText(add);
		    	if((add=result.getString(6))!=null)
	        		   textDob.setText(add);
		    	if((add=result.getString(7))!=null)
	        		   textPermAddrs.setText(add);
		    	if((add=result.getString(9))!=null)
	        		   textNumber.setText(add);
		    	if((id=result.getInt(12))!=null){
		    		
	       		}
		    	if((add=result.getString(11))!=null){
	       			for(int i=0;i<pos.length;i++){
	       				
	       				if(add.equals(pos[i])){
	       					comboPos.setSelectedIndex(i);
	       				break;
	       				}
	       			}
	       			
	       		
		    	
		    	
		    		
		    	
		    }
		    }
		    	if(id!=null){
		    	ResultSet de = stat.executeQuery("SELECT Name FROM DEPT WHERE Dept_ID='"+id+"'");
	    		while(de.next()){
       			for(int i=0;i<dept.length;i++){
       				if(de.getString(1).equals(dept[i]))
       					comboDept.setSelectedIndex(i);
       			}
	    		}
		    	}
	    		
		    	
		    
		    ResultSet a = stat.executeQuery("SELECT Letter FROM SECTION WHERE Instructor_Username='"+user+"'");
    		int i=0;
    		String sec="";
		    while(a.next()){
   			
   				 sec = a.getString(1);
   				 System.out.println(sec+ "i");
   				 i=0;
   				for(String x : charSections){
   					System.out.println(x);
   					System.out.println((sec.equals(x)));
   					if(sec.equals(x ))
   						sections[i].setSelected(true);
   					i++;
   					
   				}
   				
   			
   		}
		    List<String> interests = new LinkedList<String>();
		    ResultSet intrs = stat.executeQuery("SELECT Research_Interest FROM RESEARCH_INTEREST WHERE Instructor_Username='"+user+"'");
		    while(intrs.next()){
		    	interests.add(intrs.getString(1));
		    }
		    StringBuilder res = new StringBuilder();
		    for(String x:interests){
		    	res.append(x);
		    	res.append(",");
		    }
		    textResearch.setText(res.substring(0,res.length()-1));
		    
		}catch(Exception e){
			//JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
		}
		
		btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
            	if (textName.getText().matches("((19|20)\\d{2})-([1-9]|0[1-9]|1[0-2])-(0[1-9]|[1-9]|[12][0-9]|3[01])")){
            		JOptionPane.showMessageDialog(null,"Enter correct date format 'YYYY-MM-DD'");;
            		System.out.println("workss");
            	}
            	try {
            		 ResultSet res;
            		
                	Class.forName("com.mysql.jdbc.Driver").newInstance();
                	 Connection con = Connect.getConnection();
                	
                	 Statement statement =con.createStatement();
                	

                	 String name = "'"+textName.getText()+"',";
                	 
                	 String dob = "'"+textDob.getText()+"',";
                	 
                	 String gender ="'"+comboGender.getSelectedItem().toString()+"'"+",";
                	
                	 String addrs ="'"+textAddrs.getText().toString()+"'"+",";
                  	 String perm ="'"+textPermAddrs.getText()+"'"+",";
                	 String dept ="'"+comboDept.getSelectedItem().toString()+"'"+",";
                	 String pos ="'"+comboPos.getSelectedItem().toString()+"'"+",";
                	 String number = textNumber.getText();
                	 String email = "'"+textEmail.getText()+"'";
                	 
                	 
                	
                	 statement.executeUpdate("UPDATE FACULTY SET Name = "+name + "Gender = " + gender+ "Position =" + pos+ "DOB =" + dob + "Addrs= " + addrs +"Perm_Addrs= "+perm+"Contact_No= '" +number +"'," + "Email_ID =" + email +" WHERE Username='"+ user +"'");
                	 String x="";
                	 ResultSet retur=statement.executeQuery("SELECT Course_Title FROM CRS_CODE WHERE Code='"+comboBox_3.getSelectedItem().toString()+ "'");
                	 while(retur.next()){
             			x=retur.getString(1);
             	}
                	 System.out.println(x);
                	
                	 if(A.isSelected()){
                	 statement.executeUpdate("UPDATE SECTION SET Instructor_Username='" +user +"' WHERE Course_Title='"+ x +"' AND Letter='A'");
                	 }
                	 if(B.isSelected()){
                    	 statement.executeUpdate("UPDATE SECTION SET Instructor_Username='" +user +"' WHERE Course_Title='"+ x +"' AND Letter='B'");
                    	 }
                	 if(C.isSelected()){
                    	 statement.executeUpdate("UPDATE SECTION SET Instructor_Username='" +user +"' WHERE Course_Title='"+ x +"' AND Letter='C'");
                    	 }
                	 if(D.isSelected()){
                    	 statement.executeUpdate("UPDATE SECTION SET Instructor_Username='" +user +"' WHERE Course_Title='"+ x +"' AND Letter='D'");
                    	 }
                	 List<String> list =  Arrays.asList(textResearch.getText().toString().split(","));
                	 
                	 statement.executeUpdate("DELETE FROM RESEARCH_INTEREST WHERE Instructor_Username='" + user +"'");
                	 for(String ri:list){
                		 
                		 statement.executeUpdate("INSERT INTO RESEARCH_INTEREST VALUES('" + ri +"','"+user+"')");
                	 }
                	
                	 retur = statement.executeQuery("SELECT Dept_ID FROM DEPT AS D WHERE D.Name='" +comboDept.getSelectedItem().toString()+"'");
                	if(retur.next()){
                		
                		statement.executeUpdate("UPDATE FACULTY SET Dept_Id='"+ retur.getInt(1) +"'" +" WHERE Username='"+ user +"'");
                		
                	}
                	dispose();
                	new FacultyServices(user);
                	
                	
                	
            	}
               catch(Exception eb) {
            	   //JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
            	} finally {
            	};

	}
});
}
}
}
